package com.guc.coroutines

import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.guc.coroutines.adapter.FileCacheAdapter
import com.guc.coroutines.databinding.ActivityDatabaseBinding
import com.guc.coroutines.db.FileCache
import com.guc.coroutines.db.ImageCacheUtil
import com.guc.coroutines.vm.DatabaseViewModel
import com.guc.gviewbinding.inflate
import kotlin.concurrent.thread

class DatabaseActivity : AppCompatActivity() {

    private val binding: ActivityDatabaseBinding by inflate()
    private lateinit var fileCacheAdapter:FileCacheAdapter

    private val viewModel: DatabaseViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.rcvContent.apply {
            layoutManager = LinearLayoutManager(this@DatabaseActivity)
            fileCacheAdapter = FileCacheAdapter(arrayListOf())
            adapter = fileCacheAdapter
        }
        viewModel.fileCache.observe(this){
            fileCacheAdapter.update(it)
        }
        binding.btnInsert.setOnClickListener {
            insert()
        }
        binding.btnQuery.setOnClickListener {
            query()
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            android.R.id.home->finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.database_menu,menu)
        val search = menu.findItem(R.id.menu_search)
        val searchView = search.actionView as SearchView
        searchView.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                Toast.makeText(this@DatabaseActivity,query?:"未输入",Toast.LENGTH_SHORT).show()
                viewModel.queryById(query!!)
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                Log.e("searchView",newText?:"")
                return false
            }

        })
        return super.onCreateOptionsMenu(menu)
    }
    /**
     * 插入数据
     */
    private fun insert(){
        if (binding.etId.text.toString().isEmpty()||binding.etPath.text.toString().isEmpty()){
            binding.tvHint.text = "数据不能为空";
            return
        }
        val id = binding.etId.text.toString()
        thread {
            if (ImageCacheUtil.getCacheDao().getCacheByFileId(id)==null){
                ImageCacheUtil.getCacheDao().insertAll(
                    FileCache(id,binding.etPath.text.toString(),System.currentTimeMillis())
                )
                binding.tvHint.text="插入成功"
            }else{
                binding.tvHint.text="数据已经存在，插入失败"
            }
        }
    }

    /**
     * 查询数据库
     */
    private fun query(){
        viewModel.getAll()
    }
}